package com.zwb.aiwebsite.service.impl;

import com.zwb.aiwebsite.dto.ProductDTO;
import com.zwb.aiwebsite.entity.product.TbProduct;
import com.zwb.aiwebsite.entity.product.TbProductInfo;
import com.zwb.aiwebsite.entity.product.TbProductType;
import com.zwb.aiwebsite.entity.solution.TbSolutionType;
import com.zwb.aiwebsite.repository.TbProductInfoRepository;
import com.zwb.aiwebsite.repository.TbProductRepository;
import com.zwb.aiwebsite.repository.TbProductTypeRepository;
import com.zwb.aiwebsite.service.TbProductService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @author 2825075112@qq.com
 */
@Service
@AllArgsConstructor
public class TbProductServiceImpl implements TbProductService {


    private final TbProductTypeRepository tbProductTypeRepository;
    private final TbProductInfoRepository tbProductInfoRepository;
    private final TbProductRepository tbProductRepository;

    @Override
    public void addProductType(TbProductType tbProductType) {
        if (StringUtils.isBlank(tbProductType.getName())){
            return;
        }
        if (tbProductType.getOrder() == null){
            tbProductType.setOrder(1);
        }
        tbProductTypeRepository.save(tbProductType);
    }

    @Override
    public List<TbProductType> getAllProductType() {
        return tbProductTypeRepository.findAll();
    }

    @Override
    public void delProductType(Integer id) {
         tbProductTypeRepository.deleteById(id);
    }

    @Override
    public List<TbProduct> getAllProduct() {
        return tbProductRepository.findAll();
    }

    @Override
    public void delProduct(Integer id) {
        tbProductRepository.deleteById(id);
        tbProductInfoRepository.deleteById(id);
    }

    @Override
    public void addProduct(ProductDTO productDTO) {
        if (StringUtils.isBlank(productDTO.getName())){
            return;
        }
        if (StringUtils.isBlank(productDTO.getImgUrl())){
            return;
        }
        if (StringUtils.isBlank(productDTO.getContent())){
//            return;
        }
        if (productDTO.getOrder() == null){
            productDTO.setOrder(1);
        }
        TbProduct tbProduct = new TbProduct();
        TbProductInfo tbProductInfo = new TbProductInfo();
        BeanUtils.copyProperties(productDTO, tbProduct);
        BeanUtils.copyProperties(productDTO, tbProductInfo);
        tbProductRepository.save(tbProduct);
        tbProductInfoRepository.save(tbProductInfo);
    }

    @Override
    public void productMenu(Integer id, Model model) {
        Optional<TbProductType> tbProductTypeOptional = tbProductTypeRepository.findById(id);
        if (!tbProductTypeOptional.isPresent()){
            return;
        }
        TbProductType tbProductType = tbProductTypeOptional.get();
        model.addAttribute("productTypeName", tbProductType.getName());
        List<TbProduct> all = tbProductRepository.findAllByProductType(id);
        all.sort(Comparator.comparing(TbProduct::getOrder));
        model.addAttribute("productList", all);
    }

    @Override
    public List<ProductDTO> getAllProductInfo() {
        List<TbProduct> all = tbProductRepository.findAll();
        List<ProductDTO> result = new ArrayList<>();
        for (TbProduct tbProduct : all) {
            ProductDTO productDTO = new ProductDTO();
            BeanUtils.copyProperties(tbProduct, productDTO);
            Optional<TbProductInfo> tbProductInfoOptional = tbProductInfoRepository.findById(tbProduct.getId());
            if (tbProductInfoOptional.isPresent()) {
                TbProductInfo tbProductInfo = tbProductInfoOptional.get();
                BeanUtils.copyProperties(tbProductInfo, productDTO);
            }
            result.add(productDTO);
        }
        return result;
    }

    @Override
    public void productInfoPage(Model model, Integer id) {
        Optional<TbProductInfo> tbProductInfoOptional = tbProductInfoRepository.findById(id);
        if (tbProductInfoOptional.isPresent()) {
            TbProductInfo tbProductInfo = tbProductInfoOptional.get();
            model.addAttribute("tbProductInfo", tbProductInfo);
        }
    }
}
